package cn.iocoder.yudao.module.system.dal.mysql.portal;



import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.portal.vo.PortalUserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.portal.vo.PortalUserRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.portal.PortalUserDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 门户网站用户 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface PortalUserMapper extends BaseMapperX<PortalUserDO> {

    default PageResult<PortalUserDO> selectPage(PortalUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PortalUserDO>()
                .likeIfPresent(PortalUserDO::getName, reqVO.getName())
                .likeIfPresent(PortalUserDO::getNickName, reqVO.getNickName())
                .eqIfPresent(PortalUserDO::getTelePhone, reqVO.getTelePhone())
                .eqIfPresent(PortalUserDO::getWechat, reqVO.getWechat())
                .eqIfPresent(PortalUserDO::getEmail, reqVO.getEmail())
                .eqIfPresent(PortalUserDO::getPassword, reqVO.getPassword())
                .eqIfPresent(PortalUserDO::getUserType, reqVO.getUserType())
                .eqIfPresent(PortalUserDO::getDelFlag, reqVO.getDelFlag())
                .betweenIfPresent(PortalUserDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(PortalUserDO::getModifyTime, reqVO.getModifyTime())
                .eqIfPresent(PortalUserDO::getCreatorId, reqVO.getCreatorId())
                .eqIfPresent(PortalUserDO::getOrgCode, reqVO.getOrgCode())
                .orderByDesc(PortalUserDO::getId));
    }


    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return 用户信息，不存在则返回 null
     */
    default PortalUserDO selectByUsername(String username) {
        return selectOne(new QueryWrapper<PortalUserDO>()
                .eq("name", username)
                .eq("del_flag", "0"));
    }

    /**
     * 更新用户最后登录信息
     *
     * @param id  userId
     * @param lastLoginIp  最后登录IP
     * @param lastLoginTime 最后登录时间
     */
    void updateLoginInfo(@Param("id") Long id,
                         @Param("lastLoginIp") String lastLoginIp,
                         @Param("lastLoginTime") LocalDateTime lastLoginTime);

    List<PortalUserRespVO> selectUserList();

    String getUserNameByName(String auditor);
}